/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Data;
import model.db.OurConnection;
import model.db.table.Bid;
import model.db.table.BidHistory;

/**
 * Author: Raoux Etienne
 * Student login: jc218934
 * Student number: 12448190
 * Date: Jul 31, 2010
 * Time: 9:46:32 AM
 * email: etienne.raoux@my.jcu.edu.au
 * alt email: jc218934@gmail.com
 */
public class UCDisplayAllItems implements UCController
{

    public void run()
    {
        System.out.println("Displaying items stored in db");
        Thread thread = new Thread(new DisplayDb());
        thread.start();

    }

    private class DisplayDb implements  Runnable
    {

        public void run()
        {
            String strToExecute = "SELECT * FROM BID_HISTORY";
            Connection con = null;
            ArrayList<BidHistory> bidHistoryList = new ArrayList<BidHistory>();
            try
            {
                con = OurConnection.connect();
                Statement stmt = con.createStatement();
                ResultSet resultset = stmt.executeQuery(strToExecute);

                while (resultset.next())
                {

                    Integer itemId = resultset.getInt(1);
                    String imgUrl = resultset.getString(2);
                    String title = resultset.getString(3);
                    String itemUrl = resultset.getString(4);
                    String seller = resultset.getString(5);
                    String sellerUrl = resultset.getString(6);
                    Double price = resultset.getDouble(7);
                    ArrayList<Bid> bidHistory = new ArrayList<Bid>();

                    

                    BidHistory item = new BidHistory(itemId, imgUrl, title, itemUrl, seller, sellerUrl, price, bidHistory);
                    bidHistoryList.add(item);
                    Data.urlExtractorListener.addItem(item);
                }
                
                for(BidHistory item:bidHistoryList)
                {
                    String strToExecute2 = "SELECT * FROM BID WHERE ITEM_ID = "+item.getItemId();
                    ResultSet resultset2 = stmt.executeQuery(strToExecute2);
                    while (resultset2.next())
                    {
                        Integer id = resultset2.getInt(1);
                        Integer bidHistoryId = resultset2.getInt(2);
                        Date date  = resultset2.getDate(3);
                        String bidder  = resultset2.getString(4);
                        String bidType  = resultset2.getString(5);
                        Integer qty  = resultset2.getInt(6);
                        Double amt = resultset2.getDouble(7);


                        item.add(new Bid(id, bidHistoryId, date, bidder, bidType, qty, amt));
                        
                    }
                }


                con.close();

            }
            catch (Exception ex)
            {
                try {
                    con.close();
                } catch (SQLException ex1) {
                }
                ex.printStackTrace();
            }

        }

    }

}
